package net.net23.sysdent.client.ui.widgets.links;

import net.net23.sysdent.client.controller.AppController;
import net.net23.sysdent.client.services.LinksServiceAsync;
import net.net23.sysdent.client.ui.widgets.dialog.T4LDialogHandler;
import net.net23.sysdent.shared.jto.Link;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class EditLinkForm extends Composite {

	private TextBox name;
	private TextBox link;
	private FlexTable layout;
	private Link currentLink;
	private T4LDialogHandler t4lDialogHandler;

	public EditLinkForm(final T4LDialogHandler t4lDialogHandler,
			final LinksServiceAsync reloadService, Link currentLink) {
		this.t4lDialogHandler = t4lDialogHandler;
		this.currentLink = currentLink;

		boolean isNew = currentLink == null;

		name = new TextBox();
		name.setText(isNew ? "" : currentLink.getName());
		name.setWidth("100%");
		link = new TextBox();
		link.setText(isNew ? "" : currentLink.getLink());
		link.setWidth("100%");
		layout = new FlexTable();
		layout.setWidth("100%");
		layout.setCellSpacing(6);
		layout.setWidth("600px");
		FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
		cellFormatter.setWidth(0, 0, "10%");
		layout.setHTML(0, 0, "Nombre:");
		cellFormatter.setWidth(0, 1, "90%");
		layout.setWidget(0, 1, name);
		cellFormatter.setWidth(0, 2, "18px");
		layout.setWidget(0, 2, new Label(""));
		cellFormatter.setWidth(1, 0, "10%");
		layout.setHTML(1, 0, "Enlace*:");
		cellFormatter.setWidth(0, 1, "90%");
		layout.setWidget(1, 1, link);
		cellFormatter.setWidth(1, 2, "18px");
		Label label = new Label("");
		layout.setWidget(1, 2, label);

		cellFormatter.setColSpan(2, 0, 3);
		cellFormatter.setHorizontalAlignment(2, 0,
				HasHorizontalAlignment.ALIGN_CENTER);
		HorizontalPanel hp = new HorizontalPanel();
		hp.setSpacing(5);
		layout.setWidget(2, 0, hp);

		hp.add(new Button("Aceptar", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				acept(reloadService);
			}
		}));
		hp.add(new Button("Cancelar", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				cancel();
			}
		}));
		initWidget(layout);
	}

	@Override
	protected void onLoad() {
		link.getElement().focus();
	}

	protected void cancel() {
		t4lDialogHandler.close();
	}

	protected void acept(LinksServiceAsync reloadService) {
		String _name = name.getText();
		String _link = link.getText();
		boolean isNew = currentLink == null || currentLink.getId() == null
				|| currentLink.getId() <= 0;
		if (_link.trim().length() == 0) {
			Image image = new Image(AppController.images.errorMini());
			final DecoratedPopupPanel simplePopup = new DecoratedPopupPanel(
					true);
			VerticalPanel vp = new VerticalPanel();
			Label title = new Label("Error");
			title.setStyleName("t4l-bold");
			vp.add(title);
			vp.setCellHorizontalAlignment(title,
					HasHorizontalAlignment.ALIGN_CENTER);
			Label msg = new Label("Este campo es requerido");
			vp.add(msg);
			simplePopup.setWidget(vp);
			image.addMouseOverHandler(new MouseOverHandler() {
				@Override
				public void onMouseOver(MouseOverEvent event) {
					Widget source = (Widget) event.getSource();
					int left = source.getAbsoluteLeft() + 10;
					int top = source.getAbsoluteTop() + 10;
					simplePopup.setPopupPosition(left, top);
					simplePopup.show();
				}
			});
			image.addMouseOutHandler(new MouseOutHandler() {
				@Override
				public void onMouseOut(MouseOutEvent event) {
					simplePopup.hide();
				}
			});
			layout.setWidget(1, 2, image);
			link.getElement().focus();
		} else {
			if (isNew) {
				currentLink = new Link();
				currentLink.setLink(_link);
				currentLink.setName(_name);
				create(reloadService);
			} else {
				currentLink.setLink(_link);
				currentLink.setName(_name);
				update(reloadService);
			}
		}
	}

	private void create(LinksServiceAsync reloadService) {
		reloadService.create(this.currentLink, new AsyncCallback<Void>() {
			@Override
			public void onSuccess(Void result) {
				cancel();
			}

			@Override
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
			}
		});
	}

	private void update(LinksServiceAsync reloadService) {
		reloadService.update(this.currentLink, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
			}

			@Override
			public void onSuccess(Void result) {
				cancel();
			}
		});
	}
}
